<template>
  <div>
    <div>
      <div class="handle-box">
        <el-select
          v-model="pagination.forbidden"
          placeholder="用户状态"
          class="handle-select mrb10"
        >
          <el-option key="0" label="启用" :value="false"></el-option>
          <el-option key="1" label="禁用" :value="true"></el-option>
        </el-select>
        <el-input
          v-model="pagination.username"
          placeholder="用户名"
          class="handle-input mrb10"
        ></el-input>
        <el-input
          v-model="pagination.email"
          placeholder="邮箱"
          class="handle-input mrb10"
        ></el-input>
        <el-button type="primary" icon="el-icon-search" @click="getUsers()">搜索</el-button>
        <el-button type="danger" @click="clearSearch()">清除参数</el-button>
      </div>
      <el-table
        :data="users"
        border
        class="table"
        header-cell-class-name="table-header"
      >
        <el-table-column
          prop="username"
          label="用户名"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="email"
          label="邮箱"
          align="center"
          width="160px"
        ></el-table-column>

        <el-table-column label="用户状态" align="center">
          <template slot-scope="scope">
            <el-tag
              :type="scope.row.forbidden === false ? 'success' : 'danger'"
              disable-transitions
            >
              {{ scope.row.forbidden === false ? "启用" : "禁用" }}
            </el-tag>
            <el-switch
              @click.native="changeforbidden(scope.row)"
              v-model="scope.row.forbidden"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="头像" align="center">
          <template slot-scope="scope">
            <el-image
              lazy
              class="table-td-thumb"
              :src="scope.row.avatar"
              fit="cover"
            ></el-image>
          </template>
        </el-table-column>
        <el-table-column label="性别" align="center">
          <template slot-scope="scope">
            <el-tag
              type="success"
              v-if="scope.row.gender == 1"
              disable-transitions
            >
              男
            </el-tag>
            <el-tag
              type="success"
              v-else-if="scope.row.gender == 2"
              disable-transitions
            >
              女
            </el-tag>
            <el-tag type="success" v-else disable-transitions> 保密</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          prop="introduction"
          label="简介"
          align="center"
        ></el-table-column>
        <el-table-column label="用户类型" align="center">
          <template slot-scope="scope">
            <el-tag
              type="success"
              v-if="scope.row.role == 'administrator'"
              disable-transitions
            >
              超级管理员
            </el-tag>
            <el-tag
              type="success"
              v-else-if="scope.row.role == 'common'"
              disable-transitions
            >
              普通用户
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          prop="createTime"
          label="注册时间"
          align="center"
          width="160px"
        ></el-table-column>
        <el-table-column label="操作" width="270" align="center">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.row.id)"
            >编辑
            </el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row.id)"
            >删除
            </el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
          background
          layout="total, prev, pager, next"
          :current-page="pagination.current"
          :page-size="pagination.size"
          :total="pagination.total"
          @current-change="handlePageChange"
        >
        </el-pagination>
      </div>
    </div>

    <!-- 编辑弹出框 -->
    <el-dialog
      title="用户详情"
      :visible.sync="editVisible"
      width="30%"
      :append-to-body="true"
      destroy-on-close
      center
    >
      <div class="myCenter">
        <el-form
          ref="form"
          label-width="75px"
        >
          <el-form-item label="用户名">
            <el-input v-model="form.username"></el-input>
          </el-form-item>
          <el-form-item label="邮箱">
            <el-input v-model="form.email"></el-input>
          </el-form-item>
          <el-form-item label="性别">
            <el-select v-model="form.gender" placeholder="请选择性别">
              <el-option label="女" value="2"></el-option>
              <el-option label="男" value="1"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="用户类型">
            <el-select v-model="form.role" placeholder="请选择用户类型">
              <el-option label="超级管理员" value="administrator"></el-option>
              <el-option label="普通用户" value="common"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>

      <span slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveEdit()">保存</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      pagination: {
        current: 1,
        size: 10,
        total: 0,
        forbidden: null,
        username: null,
        email: null,
      },
      form: {},
      users: [],
      changeUser: {
        id: null,
        userType: null,
      },
      editVisible: false
    };
  },

  computed: {},

  watch: {},

  created() {
    this.getUsers();
  },

  mounted() {
  },

  methods: {
    clearSearch() {
      this.pagination = {
        current: 1,
        size: 10,
        total: 0,
        forbidden: null,
        username: null,
        email: null
      };
      this.getUsers();
    },
    getUsers() {
      this.$createHttp.userPage(this.pagination).then((res) => {
        if (res.code === 200) {
          if (!this.$common.isEmpty(res.data)) {
            this.users = res.data.records;
            this.pagination.total = res.data.total;
          }
        } else {
          this.$message({
            message: res.msg,
            type: "error",
          });
        }
      });
    },
    changeforbidden(user) {
      this.$createHttp
        .changeForbidden({
          id: user.id,
          forbidden: user.forbidden,
        })
        .then((res) => {
          if (res.code === 200) {
            this.$message({
              message: "修改成功！",
              type: "success",
            });
          } else {
            this.$message({
              message: res.msg,
              type: "error",
            });
          }
        });
      this.getUsers();
    },
    handleEdit(id) {
      this.$createHttp.userInfo(id).then((res) => {
        if (res.code === 200) {
          this.form = res.data;
          this.editVisible = true;
        } else {
          this.$message({
            message: res.msg,
            type: "error",
          });
        }
      });
    },
    handleDelete(id) {
      this.$confirm("此操作将删除该用户, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        this.$createHttp.userDelete(id).then((res) => {
          if (res.code === 200) {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            setTimeout(() => {
              this.getUsers();
            });
          } else {
            this.$message({
              type: "error",
              message: res.msg,
            });
          }
        });
      });
    },
    handlePageChange(val) {
      this.pagination.current = val;
      this.getUsers();
    },
    //保存修改
    saveEdit() {
      this.$createHttp.updateUser(this.form).then(res => {
        if (res.code === 200) {
          this.$message({
            message: '修改成功',
            type: 'success'
          })
          this.editVisible = false
          setTimeout(() => {
            this.getUsers()
          })
        } else {
          this.$message({
            message: res.msg,
            type: 'error'
          })
        }
      })
    },
  },
};
</script>

<style scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 160px;
  display: inline-block;
}

.table {
  width: 100%;
  font-size: 14px;
}

.mrb10 {
  margin-right: 10px;
  margin-bottom: 10px;
}

.table-td-thumb {
  display: block;
  margin: auto;
  width: 40px;
  height: 40px;
}

.pagination {
  margin: 20px 0;
  text-align: right;
}
</style>
